@echo off
REM ========================================================================
REM JAR Update Tool - Windows EXE Build Script
REM ========================================================================

setlocal enabledelayedexpansion

echo.
echo ================================================================
echo          JAR Update Tool - Windows EXE Build Script
echo ================================================================
echo.

REM Check current directory
if not exist "src\main.py" (
    echo [ERROR] Please run this script from project root directory!
    echo Current directory: %CD%
    echo Required file: src\main.py
    pause
    exit /b 1
)

REM Check Python installation
echo [1/6] Checking Python environment...
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not installed. Please install Python 3.7+
    echo Download: https://www.python.org/downloads/
    pause
    exit /b 1
)

python --version
echo [OK] Python installed

REM Check required files
echo.
echo [2/6] Checking required files...

if not exist "docs\img\update.png" (
    echo [ERROR] Icon file not found: docs\img\update.png
    pause
    exit /b 1
)
echo [OK] Icon file: docs\img\update.png

if not exist "tools\UnRAR.exe" (
    echo [WARNING] UnRAR.exe not found: tools\UnRAR.exe
    echo RAR extraction will not be available
) else (
    echo [OK] UnRAR tool: tools\UnRAR.exe
)

REM Install dependencies
echo.
echo [3/6] Installing build dependencies...
echo Installing PyInstaller and Pillow...

python -m pip install --upgrade pip --quiet
python -m pip install pyinstaller --quiet
python -m pip install Pillow --quiet

if errorlevel 1 (
    echo [ERROR] Failed to install dependencies
    pause
    exit /b 1
)
echo [OK] Dependencies installed

REM Convert icon (PNG to ICO)
echo.
echo [4/6] Preparing icon file...

python -c "from PIL import Image; img = Image.open('docs/img/update.png'); img.save('docs/img/update.ico', format='ICO')" 2>nul

if exist "docs\img\update.ico" (
    echo [OK] Icon converted: docs\img\update.ico
    set ICON_FILE=docs\img\update.ico
) else (
    echo [WARNING] Icon conversion failed, using PNG
    set ICON_FILE=docs\img\update.png
)

REM Clean old build files
echo.
echo [5/6] Cleaning old build files...
if exist "build" (
    echo Deleting build directory...
    rmdir /s /q build
)
if exist "dist" (
    echo Deleting dist directory...
    rmdir /s /q dist
)
if exist "*.spec" (
    echo Deleting old spec files...
    del /q *.spec 2>nul
)
echo [OK] Cleanup completed

REM Build EXE
echo.
echo [6/6] Building executable file...
echo This may take several minutes, please wait...
echo.

pyinstaller ^
    --name="JAR-Updater" ^
    --icon="%ICON_FILE%" ^
    --onefile ^
    --windowed ^
    --clean ^
    --noconfirm ^
    --add-data="templates;templates" ^
    --add-data="static;static" ^
    --add-data="tools;tools" ^
    --hidden-import=openpyxl ^
    --hidden-import=xlrd ^
    --hidden-import=pandas ^
    --hidden-import=flask ^
    --hidden-import=werkzeug ^
    --hidden-import=jinja2 ^
    --hidden-import=rarfile ^
    --hidden-import=tkinter ^
    --hidden-import=tkinter.filedialog ^
    --hidden-import=tkinter.ttk ^
    --hidden-import=tkinter.messagebox ^
    --hidden-import=tkinter.scrolledtext ^
    src/main.py

if errorlevel 1 (
    echo.
    echo ================================================================
    echo                      BUILD FAILED!
    echo ================================================================
    echo.
    echo Possible reasons:
    echo 1. Missing Python dependencies: pip install -r config\requirements.txt
    echo 2. PyInstaller version issue: pip install --upgrade pyinstaller
    echo 3. File path issue - ensure all files exist
    echo.
    pause
    exit /b 1
)

REM Verify output file
if not exist "dist\JAR-Updater.exe" (
    echo.
    echo [ERROR] Executable file was not generated
    pause
    exit /b 1
)

REM Display file size
echo.
echo ================================================================
for %%I in ("dist\JAR-Updater.exe") do set SIZE=%%~zI
set /a SIZE_MB=!SIZE! / 1024 / 1024
echo File size: !SIZE_MB! MB

REM Build completed
echo.
echo ================================================================
echo                    BUILD SUCCESSFUL!
echo ================================================================
echo.
echo [OK] Executable: dist\JAR-Updater.exe
echo [OK] File size: !SIZE_MB! MB
echo [OK] Icon: %ICON_FILE%
echo [OK] UnRAR.exe included
echo.
echo ----------------------------------------------------------------
echo Usage Instructions:
echo ----------------------------------------------------------------
echo.
echo 1. Copy dist\JAR-Updater.exe to target Windows PC
echo 2. Double-click to run (first run may take 5-10 seconds)
echo 3. Supports drag and drop for JAR, ZIP/RAR, Excel files
echo.
echo ----------------------------------------------------------------
echo Notes:
echo ----------------------------------------------------------------
echo.
echo 1. May be blocked by antivirus - add to whitelist
echo 2. Target PC may need Visual C++ Redistributable
echo    Download: https://aka.ms/vs/17/release/vc_redist.x64.exe
echo 3. UnRAR.exe is included for RAR extraction
echo 4. All templates and static resources are packaged
echo.
echo ================================================================

REM Ask to open output directory
echo.
set /p OPEN_DIR="Open output directory? (Y/N): "
if /i "%OPEN_DIR%"=="Y" (
    explorer dist
)

REM Ask to clean build files
echo.
set /p CLEAN_BUILD="Clean temporary build files? (Y/N): "
if /i "%CLEAN_BUILD%"=="Y" (
    echo Cleaning...
    if exist "build" rmdir /s /q build
    if exist "*.spec" del /q *.spec 2>nul
    echo [OK] Cleanup completed
)

echo.
echo Press any key to exit...
pause >nul




